<template>
  <view class="invoice" v-if="invoiceConf">
    <view class="invoice-list">
      <view class="invoice-one" @click="show = true">
        <view>开具发票</view>
        <view>
          <view>{{ hasInvoice ? getType : "本次不开具发票" }}</view>
          <image :src="asyncImgs.my.icon_right_3" mode=""></image>
        </view>
      </view>
    </view>

    <u-popup
      safe-area-inset-bottom
      :mask-close-able="false"
      closeable
      mode="bottom"
      v-model="show"
      border-radius="22"
    >
      <view class="pop-con">
        <view class="pop-tit">
          <view class="title">发票</view>
          <view class="notice" @click="noticeShow = true">
            <text>发票须知</text>
            <i class="iconfont icon-help1"></i>
          </view>
        </view>

        <scroll-view scroll-y>
          <view class="subtit">发票类型</view>
          <view class="radio">
            <template v-for="item of options">
              <view
                :key="item.value"
                v-if="invoiceConf.conf.types.includes(item.value)"
                :class="['item', form.type == item.value ? 'on' : '']"
                @click="handle('type', item.value)"
                >{{ item.label }}</view
              >
            </template>
            <!-- <view class="item">专用发票</view> -->
          </view>
          <view class="ts"
            >部分主体交付数电普通发票，其法律效力与电子普通发票相同</view
          >
          <view class="line"></view>

          <view class="subtit">开票方式</view>
          <view class="radio">
            <view
              v-for="item of options2"
              :key="item.value"
              :class="['item', form.has_apply == item.value ? 'on' : '']"
              @click="handle('has_apply', item.value)"
              >{{ item.label }}</view
            >
          </view>
          <view class="ts">发票将与商品分开邮寄</view>
          <view class="line"></view>

          <view class="subtit">{{
            form.type == 2 ? "增值资质" : "发票抬头"
          }}</view>
          <view class="inpuText">
            <view class="let">单位名称</view>
            <view class="rig">
              <input
                v-model="form.title"
                type="text"
                :placeholder-style="placeholderStyle"
                placeholder="请输入单位名称"
                @blur="searchInvoiceNo"
              />
            </view>
          </view>
          <view class="inpuText">
            <view class="let">纳税人识别号</view>
            <view class="rig">
              <input
                v-model="form.code"
                type="text"
                :placeholder-style="placeholderStyle"
                placeholder="请输入纳税人识别号"
              />
            </view>
          </view>
          <template v-if="form.type == 2">
            <view class="inpuText">
              <view class="let">注册地址</view>
              <view class="rig">
                <input
                  v-model="form.ext.address"
                  type="text"
                  :placeholder-style="placeholderStyle"
                  placeholder="请输入注册地址"
                />
              </view>
            </view>
            <view class="inpuText">
              <view class="let">注册电话</view>
              <view class="rig">
                <input
                  v-model="form.ext.mobile"
                  type="tel"
                  :placeholder-style="placeholderStyle"
                  placeholder="请输入注册电话"
                />
              </view>
            </view>
            <view class="inpuText">
              <view class="let">开户银行</view>
              <view class="rig">
                <input
                  v-model="form.ext.bank"
                  type="text"
                  :placeholder-style="placeholderStyle"
                  placeholder="请输入开户银行"
                />
              </view>
            </view>
            <view class="inpuText">
              <view class="let">银行账户</view>
              <view class="rig">
                <input
                  v-model="form.ext.bank_card"
                  type="tel"
                  :placeholder-style="placeholderStyle"
                  placeholder="请输入银行账户"
                />
              </view>
            </view>
          </template>
          <view class="line"></view>

          <view class="subtit">收票人信息</view>
          <view class="inpuText">
            <view class="let">收票人邮箱</view>
            <view class="rig">
              <input
                v-model="form.email"
                type="email"
                :placeholder-style="placeholderStyle"
                placeholder="请输入收票人邮箱"
              />
            </view>
          </view>
          <view class="line"></view>

          <view class="subtit"
            >发票内容
            <text>发票内容选项已根据税法调整，具体请以展示为准</text>
          </view>
          <view class="radio">
            <view
              v-for="item of options3"
              :key="item.value"
              :class="['item', form.desc_type == item.value ? 'on' : '']"
              @click="handle('desc_type', item.value)"
              >{{ item.label }}</view
            >
          </view>
          <view class="ts"
            >发票内容将显示详情商品名称与价格信息，发票金额为实际支付
            金额，不含虚拟资产、优惠券等扣减金额</view
          >
          <view class="line"></view>
        </scroll-view>
      </view>

      <view class="confrim">
        <view @click="switchPop(false)" class="no">不开发票</view>
        <view @click="switchPop(true)">提交申请</view>
      </view>
    </u-popup>

    <u-popup
      safe-area-inset-bottom
      :mask-close-able="false"
      mode="center"
      v-model="noticeShow"
      border-radius="22"
    >
      <view class="noticeCon">
        <view class="pop-tit">
          <view class="title">发票须知</view>
        </view>

        <scroll-view scroll-y>
          {{ invoiceConf.conf.rule }}
        </scroll-view>
        <view class="notice-btn">
          <view class="btn" @click="noticeShow = false">取消</view>
          <view class="btn on" @click="noticeShow = false">我知道了</view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import { regEmail } from "@/common/regExp";
import { config, searchInvoiceNo } from "@/api/invoice";

export default {
  props: {
    invoiceConf: {
      type: Object,
      default() {
        return null;
      }
    }
  },

  computed: {
    getType() {
      return `电子（${this.form.desc_type == 1 ? "商品明细" : "商品类别"}）`;
    }
  },

  watch: {
    invoiceConf: {
      async handler(e) {
        if (e.conf) {
          this.form.type = e.conf.types[0];
          let infores = await config();
          if (!infores.code && infores.data.length) {
            let item = infores.data[0];

            item.ext = JSON.parse(item.ext);
            this.form = { ...item, has_apply: 0 };
          }
        }
      },
      deep: true,
      immediate: true
    }
  },

  data() {
    return {
      options: [
        { label: "增值税普通发票", value: 1 },
        { label: "增值税专用发票", value: 2 }
      ],
      options2: [
        { label: "集中开票", value: 0 },
        { label: "订单完成后开票", value: 1 }
      ],
      options3: [
        { label: "商品明细", value: 1 },
        { label: "商品类别", value: 2 }
      ],
      show: false,
      hasInvoice: false,
      placeholderStyle: "#8C8C8C",
      noticeShow: false,

      form: {
        has_apply: 0,
        type: 1,
        title: "",
        code: "",
        email: "",
        desc_type: 1,
        ext: {
          address: "",
          mobile: "",
          bank: "",
          bank_card: ""
        }
      }
    };
  },
  methods: {
    switchPop(hasInvoice) {
      if (hasInvoice) {
        let { form } = this;
        if (form.title == "") return this.$utils.toast("请填写单位名称");

        if (form.code == "") return this.$utils.toast("请填写纳税人识别号");

        if (form.type == 2) {
          if (form.ext.address == "")
            return this.$utils.toast("请填写注册地址");
          if (form.ext.mobile == "") return this.$utils.toast("请填写注册电话");
          if (form.ext.bank == "") return this.$utils.toast("请填写开户银行");
          if (form.ext.bank_card == "")
            return this.$utils.toast("请填写银行账户");
        }

        if (form.email == "") return this.$utils.toast("请填写收票人邮箱");
        if (!regEmail(form.email)) return this.$utils.toast("邮箱格式不正确");
      }

      this.hasInvoice = hasInvoice;
      this.show = false;
      this.$emit("callback", hasInvoice);
    },

    handle(key, value) {
      this.form[key] = value;
    },

    async searchInvoiceNo() {
      if (!this.invoiceConf.conf.appcode) return;

      const res = await searchInvoiceNo({
        keyword: this.form.title
      });

      if (res.result) {
        let { address, code, mobile } = res.result;
        if (code) this.form.code = code;
        if (address) this.form.ext.address = address;
        if (mobile) this.form.ext.mobile = mobile;
      }
    }
  }
};
</script>

<style lang="scss" scoped>
view {
  color: #262626;
  font-size: 26rpx;
}

.invoice {
  margin-top: 24rpx;
  padding: 30rpx 24rpx;
  border-radius: 22rpx;
  background-color: #ffffff;
}

.invoice-one {
  margin-bottom: 40rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;

  > view:last-child {
    display: flex;
    align-items: center;

    image {
      margin-left: 20rpx;
      width: 12rpx;
      height: 20rpx;
    }
  }

  .invoice-one-left {
    display: flex;

    view:last-child {
      margin-left: 14rpx;
      font-size: 20rpx;
      color: #f22d14;
      border: 1rpx solid #f2280d;
      border-radius: 4rpx;
      padding: 4rpx 12rpx;
    }
  }
}

.invoice-one:last-child {
  margin-bottom: 0rpx;
}

.confrim {
  padding: 12rpx 36rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;

  view {
    width: 48%;
    height: 76rpx;
    line-height: 76rpx;
    text-align: center;
    font-size: 30rpx;
    color: #ffffff;
    background: linear-gradient(90deg, #f22407 0%, #f84d17 100%);
    border-radius: 38rpx;

    &.no {
      background: linear-gradient(90deg, #ffb11c 0%, #ffbf14 100%);
    }
  }
}

.pop-con {
  padding: 0 36rpx;
}

.pop-tit {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 26rpx 0;

  .title {
    font-size: 32rpx;
    font-weight: bold;
  }

  .notice {
    display: flex;
    align-items: center;
    padding-right: 40rpx;
    color: #8c8c8c;
    font-size: 24rpx;
  }
}

scroll-view {
  max-height: 800rpx;
  overflow-y: auto;
}

.subtit {
  font-size: 28rpx;
  font-weight: bold;
  color: #262626;

  text {
    font-size: 24rpx;
    color: #8c8c8c;
    margin-left: 10rpx;
    font-weight: normal;
  }
}

.radio {
  display: flex;
  flex-wrap: wrap;
  padding-top: 24rpx;

  .item {
    padding: 12rpx 32rpx;
    border-radius: 32px;
    background: #f2f2f2;
    font-size: 24rpx;
    margin: 0 24rpx 24rpx 0;
    border: 1px solid #f2f2f2;

    &.on {
      background: #fcedeb;
      border-color: #f60413;
      color: #f60413;
    }
  }
}

.line {
  margin: 24rpx 0;
  background: #f8f8f8;
  height: 2rpx;
}

.ts {
  font-size: 24rpx;
  color: #8c8c8c;
}

.inpuText {
  display: flex;
  align-items: center;
  font-size: 24rpx;

  .let {
    width: 200rpx;
  }

  .rig {
    flex: 1;
    overflow: hidden;
  }

  input {
    width: 100%;
    font-size: 24rpx;
    height: 60rpx;
  }
}

.noticeCon {
  width: 80vw;
  padding: 0 50rpx 50rpx;
  box-sizing: border-box;

  .pop-tit {
    justify-content: center;
  }

  scroll-view {
    margin: 20rpx 0;
    max-height: auto;
    height: 600rpx;
    overflow-y: auto;
  }

  .notice-btn {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 48%;
      border: 1px solid #999;
      height: 76rpx;
      border-radius: 32px;
      font-size: 32rpx;
      color: #262626;
    }

    .on {
      background: #f0250e;
      color: #fff;
      border-color: #f0250e;
    }
  }
}
</style>
